from sqlalchemy import select, update, desc
from sqlalchemy.ext.asyncio import AsyncSession

from module_stock.entity.do.account_do import Account
from module_stock.entity.vo.account_vo import AccountModel, AccountPageQueryModel
from utils.page_util import PageUtil


class AccountDao:
    """
    股票账号模块数据库操作层
    """

    @classmethod
    async def get_account_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据主键ID获取股票账号详细信息

        :param db: orm对象
        :param id: 主键ID
        :return: 股票账号信息对象
        """
        account_info = (
            (
                await db.execute(
                    select(Account)
                    .where(
                        Account.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return account_info

    @classmethod
    async def get_account_detail_by_account_no(cls, db: AsyncSession, account_no: str):
        """
        根据账号获取股票账号详细信息

        :param db: orm对象
        :param account_no: 账号
        :return: 股票账号信息对象
        """
        account_info = (
            (
                await db.execute(
                    select(Account)
                    .where(
                        Account.account_no == account_no
                    )
                )
            )
            .scalars()
            .first()
        )

        return account_info

    @classmethod
    async def get_account_list(cls, db: AsyncSession, query_object: AccountPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取股票账号列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 股票账号列表信息对象
        """
        query = (
            select(Account)
            .where(
                Account.del_flag == '0',
                Account.miniqmt == query_object.miniqmt if query_object.miniqmt else True,
            )
            .order_by(desc(Account.update_time))
            .distinct()
        )
        account_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return account_list

    @classmethod
    async def add_account_dao(cls, db: AsyncSession, account: AccountModel):
        """
        新增股票账号数据库操作

        :param db: orm对象
        :param account: 股票账号对象
        :return:
        """
        db_account = Account(**account.model_dump(exclude={}))
        db.add(db_account)
        await db.flush()

        return db_account

    @classmethod
    async def edit_account_dao(cls, db: AsyncSession, account: dict):
        """
        编辑股票账号数据库操作

        :param db: orm对象
        :param account: 需要更新的股票账号字典
        :return:
        """
        await db.execute(update(Account), [account])

    @classmethod
    async def delete_account_dao(cls, db: AsyncSession, account: AccountModel):
        """
        删除股票账号数据库操作

        :param db: orm对象
        :param account: 股票账号对象
        :return:
        """
        await db.execute(
            update(Account)
            .where(Account.id == account.id)
            .values(del_flag='2', update_by=account.update_by, update_time=account.update_time)
        )
